package com.idp.web.test.controller;

import com.idp.web.test.entity.Product;
import com.idp.web.test.entity.SafeWarning;
import com.idp.web.test.entity.Stock;
import com.idp.web.test.entity.Warehouse;
import com.idp.web.test.entity.WarehouseType;
import com.idp.web.test.service.ProductService;
import com.idp.web.test.service.SafeWarningService;
import com.idp.web.test.service.StockService;
import com.idp.web.test.service.WarehouseService;
import com.idp.web.test.service.WarehouseTypeService;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.common.base.BaseController;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;

import net.sf.json.JSONObject;

/**
 * 
 * 安全库存预警controller
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-06 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 * 		<pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Controller
@RequestMapping("/safeWarning")
public class SafeWarningController extends BaseController {

	private Logger logger = Logger.getLogger(SafeWarningController.class);

	@Resource
	private SafeWarningService safeWarningService;
	@Resource
	private WarehouseTypeService warehouseTypeService;

	@Resource
	private WarehouseService warehouseService;

	@Resource
	private ProductService productService;

	@Resource
	private StockService stockService;

	/**
	 * 
	 * <pre>
	 * 	2017-06-06 Amber
	 * 	初始化查询页面
	 * </pre>
	 * 
	 * @return
	 */
	@RequestMapping("/init")
	public String init(HttpServletRequest request) {
		request.setAttribute("warehouses", warehouseService.findBySearch(new Warehouse()));
		return "shopping/safeWarning/safeWarningSearch";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-06 Amber
	 * 	分页查询
	 * </pre>
	 * 
	 * @param safeWarning
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(SafeWarning safeWarning, Page<SafeWarning> page, HttpServletRequest request) {
		SafeWarning sw = new SafeWarning();
		try {
			List<SafeWarning> safeWarnings = safeWarningService.findBySearch(sw);
			List<SafeWarning> result = new ArrayList<>();
			// 判断是否具有仓库条件查询
			Integer warehouseId = Integer.parseInt(request.getParameter("warehouseId"));
			if (warehouseId != null && warehouseId != 0) {
				for (SafeWarning s : safeWarnings) {
					if (s.getStock().getWarehouseId() == warehouseId) {
						result.add(s);
					}
				}
			} else {
				result = safeWarnings;
			}
			request.setAttribute("safeWarnings", result);
			request.setAttribute("page", safeWarningService.findByPage(safeWarning, page));
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
		}

		return "shopping/safeWarning/safeWarningList";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-06 Amber
	 * 	新增修改页面初始化
	 * </pre>
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/safeWarning")
	public String safeWarning(Integer id, HttpServletRequest request) {
		if (ValidateUtils.isNotEmpty(id)) {
			SafeWarning safeWarning = safeWarningService.getById(id);
			if (safeWarning != null) {
				request.setAttribute("safeWarning", safeWarning);
			}
		}
		// 除去已经设置过安全库存的stock信息
		try {
			List<Stock> stocks = stockService.findBySearch(null);
			List<SafeWarning> safeWarnings = safeWarningService.findBySearch(null);
			for (SafeWarning sw : safeWarnings) {
				for (Stock s : stocks) {
					if (s.getId() == sw.getStockId()) {
						stocks.remove(s);
						break;
					}
				}
			}
			request.setAttribute("stocks", stocks);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shopping/safeWarning/safeWarning";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-06 Amber
	 * 	保存
	 * </pre>
	 * 
	 * @param safeWarning
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(SafeWarning safeWarning) {
		JSONObject json = new JSONObject();
		try {
			// 修改
			if (ValidateUtils.isNotEmpty(safeWarning.getId())) {
				safeWarningService.update(safeWarning);
			}
			// 新增
			else {
				safeWarningService.add(safeWarning);
			}
			json.put("result", "save_success");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("result", "save_fail");
		}

		return json.toString();
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-06 Amber
	 * 	删除
	 * </pre>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {
		JSONObject json = new JSONObject();

		try {
			safeWarningService.delete(id);
			json.put("result", "delete_success");
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "delete_fail");
		}

		return json.toString();
	}
}
